//
//  M_HomePageViewController.m
//  Model
//
//  Created by apple on 2018/7/9.
//  Copyright © 2018年 apple. All rights reserved.
//


#import "M_HomePageViewController.h"
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "UIImageView+WebCache.h"
#import "SDCycleScrollView.h"
#import "M_HomeSouSuoViewController.h"//搜索
#import "M_PhotoMineGRViewController.h"//个人
#import "M_PhotoViewController.h"
#import "M_HomePageDetailViewController.h"
#import "M_MineHelpZXViewController.h"//客服
@interface M_HomePageViewController ()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource,SDCycleScrollViewDelegate>
/**
 *  图片数组
 */
@property (nonatomic, strong) NSMutableArray *imageArray;

/**
 *  指示label
 */
//@property (nonatomic, strong) UILabel *indicateLabel;
@property(nonatomic,strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) UIView *rootView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) NSMutableArray *databanArr;
@end

@implementation M_HomePageViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self data];
    [self bannerimg];
    self.title = @"首页";
    self.rootView = [[UIView alloc] init];
    [self.view addSubview:self.rootView];
    [self.rootView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset((KIsiPhoneX?-84:-49));
        
    }];
    self.rootView.backgroundColor = getColor(@"fe45cf");
    UIImage *img = [UIImage imageNamed:@"search"];
    UIButton *rightButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    [rightButton addTarget:self action:@selector(rightNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem=rightBarButton;

    
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img1 = [UIImage imageNamed:@"b_kefu"];
    leftBtn.frame=CGRectMake(0, 0, img1.size.width, img1.size.height);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"b_kefu"] forState:UIControlStateNormal];

    [leftBtn addTarget:self action:@selector(keFuClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftBarBtn;
    
    
    // Do any additional setup after loading the view.
}
- (void)keFuClick{
    M_MineHelpZXViewController *vc = [[M_MineHelpZXViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
 
}
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return  _dataArr;
}
- (NSMutableArray *)databanArr{
    
    if (!_databanArr) {
        _databanArr = [NSMutableArray array];
    }
    return  _databanArr;
    
    
}
- (void)data{
    

    
    [[MNetworkHandler sharedInstance]conURL:FirtImage networkType:NetWorkPOST params:nil delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            self.dataArr = returnData[@"Data"];
            NSLog(@"%@", self.dataArr);
     
             [self setupUI];
            
            
        }
        
        else {
            
//            [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];


}

- (void)bannerimg{
    
//    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
 
    
    [[MNetworkHandler sharedInstance]conURL:BanaerImage networkType:NetWorkPOST params:nil delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            self.databanArr = returnData[@"Data"];
            NSLog(@"%@", self.databanArr);
            [self creationView];

        }
        
        else {
            
//            [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
    
}



- (void)creationView{
    
    NSMutableArray *myarr=[[NSMutableArray alloc]init];
    for (int i=0; i<self.databanArr.count; i++) {
        
        NSString *str=[self.databanArr[i] objectForKey:@"image"];
//        NSString *urlN=manage(AP_HOST, str);
        
        [myarr addObject:str];
    }
    [self.cycleScrollView setLocalizationImageNamesGroup:myarr];
    [self.cycleScrollView reloadInputViews];
    
    
//    NSArray *arrMu = @[@"aaaa",@"aaaa",@"aaaa",@"aaaa",@"aaaa"];
    self.cycleScrollView=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(150)) imageNamesGroup:myarr];
    self.cycleScrollView.backgroundColor=getColor(@"fe45cf");
    self.cycleScrollView.pageControlAliment=SDCycleScrollViewPageContolAlimentCenter;
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.pageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    self.cycleScrollView.currentPageDotImage=[UIImage imageNamed:@"椭圆6"];
    self.cycleScrollView.pageDotImage=[UIImage imageNamed:@"椭圆6拷贝4"];
    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@""];
    //禁止自动滚动
    //        self.cycleScrollView.autoScroll = NO;
    
    [self.rootView addSubview:self.cycleScrollView];
    
    
}
- (void)rightNavItemClick{
    
    M_HomeSouSuoViewController *vc = [[M_HomeSouSuoViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", index);
    
    
}
- (void)setupUI {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, screenScale(150) + screenScale(20), DEF_SCREEN_WIDTH, screenScale(350))];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.1;
    pageFlowView.isCarousel = YES;
    pageFlowView.leftRightMargin = 40;
    pageFlowView.topBottomMargin = 100;
    
    pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    pageFlowView.isOpenAutoScroll = NO;
    pageFlowView.backgroundColor = getColor(@"fe45cf");
    //初始化pageControl
//    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, pageFlowView.frame.size.height - 32, DEF_SCREEN_WIDTH, 8)];
//    pageFlowView.pageControl = pageControl;
//    [pageFlowView addSubview:pageControl];
    [pageFlowView reloadData];
    
    [self.rootView addSubview:pageFlowView];
    

    
}


#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(screenScale(220), screenScale(350));
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
    
    
//     self.tabBarController.selectedIndex = 1;
//    NSString *shouYeID = [NSString stringWithFormat:@"%ld",[[self.dataArr[subIndex] objectForKey:@"typeId"] integerValue]];
//    [PublicFunction saveConfig:shouYeID forKey:@"shouYeID"];
//    NSLog(@"%@", [PublicFunction getConfigWithKey:@"shouYeID"]);
    
    M_HomePageDetailViewController *vc = [[M_HomePageDetailViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.orderStatus = [[self.dataArr[subIndex] objectForKey:@"typeId"] integerValue];
    vc.nameTit = [self.dataArr[subIndex] objectForKey:@"typeName"];
    [self.navigationController pushViewController:vc animated:YES];
    
//    NSDictionary *dic = [NSMutableDictionary dictionary];
//    NSString *str =[NSString stringWithFormat:@"%@", [self.dataArr[subIndex] objectForKey:@"typeId"]];
//    [dic setValue:str forKey:@"textOne"];
//
//    //创建通知
//
//    NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dic];
//
//    //通过通知中心发送通知
//
//    [[NSNotificationCenter defaultCenter] postNotification:notification];

    
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    
    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
//    self.indicateLabel.text = [NSString stringWithFormat:@"滚动到了第%ld张图",(long)pageNumber + 1];
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return self.dataArr.count;
//    return 8;
    
}

- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = [flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] init];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 8;
        bannerView.layer.masksToBounds = YES;
    }
    //在这里下载网络图片
//      [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:hostUrlsImg,imageDict[@"img"]]] placeholderImage:[UIImage imageNamed:@""]];
//    bannerView.mainImageView.image = self.imageArray[index];
    bannerView.mainImageView.layer.cornerRadius = 8;
    bannerView.mainImageView.layer.masksToBounds = YES;
    
    NSMutableDictionary *dic = self.dataArr[index];
    
    NSMutableArray *arr =[dic objectForKey:@"data"];
//    [bannerView updateInfo:arr];
    bannerView.picArr = arr;
    
    
    bannerView.indicateLabel.text = [dic objectForKey:@"typeName"];

//    bannerView.mainImageView.image = [UIImage imageNamed:@"cccc"];
    
    return bannerView;
}


#pragma mark --懒加载
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

//- (UILabel *)indicateLabel {
//    
//    if (_indicateLabel == nil) {
//        _indicateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,  25, DEF_SCREEN_WIDTH, 16)];
//        _indicateLabel.textColor = [UIColor whiteColor];
//        _indicateLabel.font = [UIFont systemFontOfSize:16.0];
//        _indicateLabel.textAlignment = NSTextAlignmentCenter;
//        _indicateLabel.text = @"热门推荐";
//    }
//    
//    return _indicateLabel;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
